package com.weilan.ark3.sqlite;

import com.weilan.ark3.entity.ConfigDto;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class SQLiteConfigUtils {

    public static ConfigDto query() {
        ConfigDto configDto = new ConfigDto();
        String sql = "SELECT * FROM config";
        try {
            Connection conn = DriverManager.getConnection("jdbc:sqlite:C:/db/ARK3/fenggu.db");
            PreparedStatement statement = conn.prepareStatement(sql);
            ResultSet rs = statement.executeQuery();
            configDto.setId(rs.getInt("id"));
            configDto.setPattern(rs.getString("pattern"));
            configDto.setTimezone(rs.getString("timezone"));
            configDto.setCom1State(rs.getInt("com1State"));
            configDto.setCom2State(rs.getInt("com2State"));
            configDto.setCom3State(rs.getInt("com3State"));
            configDto.setTcpState(rs.getInt("tcpState"));
            configDto.setAcuState(rs.getInt("acuState"));
            configDto.setArkDeviceNumber(rs.getInt("arkDeviceNumber"));
            configDto.setCollectingTankNumber(rs.getInt("collectingTankNumber"));
            configDto.setTcpSlaveStartId(rs.getInt("tcpSlaveStartId"));
            configDto.setTcpMasterStartId(rs.getInt("tcpMasterStartId"));
            configDto.setUcStartId(rs.getInt("ucStartId"));
            configDto.setAcuStartId(rs.getInt("acuStartId"));
            configDto.setFrequencyModulation(rs.getInt("frequencyModulation"));
            configDto.setDateStoreTime(rs.getInt("dateStoreTime"));
            configDto.setDeleteTime(rs.getInt("deleteTime"));
            configDto.setEmsAddressTime(rs.getInt("emsAddressTime"));
            configDto.setFaultStoreTime(rs.getInt("faultStoreTime"));
            configDto.setTacticsTime(rs.getInt("tacticsTime"));
            configDto.setControlId(rs.getInt("controlId"));
            configDto.setAmmeterType(rs.getString("ammeterType"));
            //statement.executeQuery();
            statement.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return configDto;
    }

    //修改数据
    public static void update(ConfigDto configDto) {
        // 获取实体类的属性值
        String pattern = configDto.getPattern();
        String timezone = configDto.getTimezone();
        Integer com1State = configDto.getCom1State();
        Integer com2State = configDto.getCom2State();
        Integer com3State = configDto.getCom3State();
        Integer tcpState = configDto.getTcpState();
        Integer acuState = configDto.getAcuState();
        Integer arkDeviceNumber = configDto.getArkDeviceNumber();
        Integer collectingTankNumber = configDto.getCollectingTankNumber();
        Integer tcpSlaveStartId = configDto.getTcpSlaveStartId();
        Integer tcpMasterStartId = configDto.getTcpMasterStartId();
        Integer ucStartId = configDto.getUcStartId();
        Integer acuStartId = configDto.getAcuStartId();
        Integer controlId = configDto.getControlId();
        int frequencyModulation = configDto.getFrequencyModulation();
        int dateStoreTime = configDto.getDateStoreTime();
        int deleteTime = configDto.getDeleteTime();
        int emsAddressTime = configDto.getEmsAddressTime();
        int faultStoreTime = configDto.getFaultStoreTime();
        int tacticsTime = configDto.getTacticsTime();
        String ammeterType = configDto.getAmmeterType();

        String sql = "UPDATE config SET pattern = ?, timezone = ?, com1State = ?, com2State = ?, com3State = ?, tcpState = ?, acuState = ?, arkDeviceNumber = ?, collectingTankNumber = ?, tcpSlaveStartId = ?, tcpMasterStartId = ?, ucStartId = ?, acuStartId = ?, controlId = ?," +
                "frequencyModulation = ?," +
                "dateStoreTime = ?," +
                "deleteTime = ?," +
                "emsAddressTime = ?," +
                "faultStoreTime = ?," +
                "tacticsTime = ?,ammeterType = ? " +
                "WHERE id = 1";

        try {
            // 连接到 SQLite 数据库
            Connection conn = DriverManager.getConnection("jdbc:sqlite:C:/db/ARK3/fenggu.db");
            PreparedStatement statement = conn.prepareStatement(sql);
            // 设置参数
            statement.setString(1, pattern);
            statement.setString(2, timezone);
            statement.setInt(3, com1State);
            statement.setInt(4, com2State);
            statement.setInt(5, com3State);
            statement.setInt(6, tcpState);
            statement.setInt(7, acuState);
            statement.setInt(8, arkDeviceNumber);
            statement.setInt(9, collectingTankNumber);
            statement.setInt(10, tcpSlaveStartId);
            statement.setInt(11, tcpMasterStartId);
            statement.setInt(12, ucStartId);
            statement.setInt(13, acuStartId);
            statement.setInt(14, controlId);
            statement.setInt(15, frequencyModulation);
            statement.setInt(16, dateStoreTime);
            statement.setInt(17, deleteTime);
            statement.setInt(18, emsAddressTime);
            statement.setInt(19, faultStoreTime);
            statement.setInt(20, tacticsTime);
            statement.setString(21, ammeterType);
            int i = statement.executeUpdate();
            if (i > 0) {
                System.out.println("更新成功！");
            } else {
                System.out.println("更新失败！");
            }
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

